﻿using System;
using System.Diagnostics.Contracts;
using Bamboo.Core;
using NHibernate;
using System.Security;

[assembly: SecurityRules(SecurityRuleSet.Level1)]
namespace Bamboo.Infrastructure.Repository.NHibernate
{
    public class RepositoryFactory : IRepositoryFactory
    {
        public RepositoryFactory(ISessionFactory sessionFactory)
        {
            Contract.Requires(sessionFactory != null);

            this.sessionFactory = sessionFactory;
        }

        protected readonly ISessionFactory sessionFactory;

        #region IDisposable Members

        private bool disposed = false;

        public void Dispose()
        {
            if (!this.disposed)
            {
                this.sessionFactory.Dispose();

                this.disposed = true;
            }
        }

        #endregion        

        #region IRepositoryFactory Members

        public IRepository CreateRepository()
        {
            return new Repository(this.sessionFactory.OpenSession());
        }

        #endregion        
    }
}
